package com.ymatou.iapi.commission.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.commission.parameter.ProductInfoModel;
import com.ymatou.iapi.commission.parameter.RefundCommissionRateRequestBean;
import com.ymatou.iapi.commission.service.RefundCommissionRateServiceCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;

public class Ts_RefundCommissionRate {
	
	private static RefundCommissionRateRequestBean refundCommissionRateRequestBean;
	private static RefundCommissionRateServiceCall refundCommissionRateServiceCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退佣接口");
	}

	@Before
	public void caseUp() {
		refundCommissionRateRequestBean = new RefundCommissionRateRequestBean();
		refundCommissionRateServiceCall = new RefundCommissionRateServiceCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(refundCommissionRateServiceCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RefundCommissionRate_001() {
		Logger.start(true,"获取单个商品的退佣信息");
		try {
			Logger.comment("前置数据准备");
			long orderId = 11111;
			String orderPaidTime = "2018-03-22 14:13:17";
			long buyerId = 1234;
			long sellerId = 22233;
			int countryId = 1;
			
			ArrayList<ProductInfoModel> productInfoList = new ArrayList<ProductInfoModel>();
			ProductInfoModel info1 = new ProductInfoModel();
			String catalogId = "catalogId1";
			String categoryId = "1001";
			String secCategoryId = "1102";
			BigDecimal productPrice = new BigDecimal(53);
			
			info1.setCatalogId(catalogId);
			info1.setCategoryId(categoryId);
			info1.setSecCategoryId(secCategoryId);
			info1.setProductPrice(productPrice);
			
			productInfoList.add(info1);
			Logger.comment("前置数据准备完毕");
			
			refundCommissionRateRequestBean.setOrderId(orderId);
			refundCommissionRateRequestBean.setOrderPaidTime(orderPaidTime);
			refundCommissionRateRequestBean.setUserId(buyerId);
			refundCommissionRateRequestBean.setSellerId(sellerId);
			refundCommissionRateRequestBean.setCountryId(countryId);
			refundCommissionRateRequestBean.setProductInfoList(productInfoList);
			
			refundCommissionRateServiceCall.setData(refundCommissionRateRequestBean);
			refundCommissionRateServiceCall.callService();
			this.verifyNormal(refundCommissionRateServiceCall, productInfoList);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RefundCommissionRate_002() {
		Logger.start(true,"获取多个商品的退佣信息");
		try {
			Logger.comment("前置数据准备");
			long orderId = 11111;
			String orderPaidTime = "2018-03-22 14:13:17";
			long buyerId = 1234;
			long sellerId = 22233;
			int countryId = 1;
			
			ArrayList<ProductInfoModel> productInfoList = new ArrayList<ProductInfoModel>();
			ProductInfoModel info1 = new ProductInfoModel();
			String catalogId = "catalogId1";
			String categoryId = "1001";
			String secCategoryId = "1102";
			BigDecimal productPrice = new BigDecimal(53);
			
			info1.setCatalogId(catalogId);
			info1.setCatalogId(categoryId);
			info1.setSecCategoryId(secCategoryId);
			info1.setProductPrice(productPrice);
			
			ProductInfoModel info2 = new ProductInfoModel();
			String catalogId2 = "catalogId2";
			String categoryId2 = "1001";
			String secCategoryId2 = "1102";
			BigDecimal productPrice2 = new BigDecimal(4);
			
			info2.setCatalogId(catalogId2);
			info2.setCategoryId(categoryId2);
			info2.setSecCategoryId(secCategoryId2);
			info2.setProductPrice(productPrice2);
			
			productInfoList.add(info1);
			productInfoList.add(info2);
			Logger.comment("前置数据准备完毕");
			
			refundCommissionRateRequestBean.setOrderId(orderId);
			refundCommissionRateRequestBean.setOrderPaidTime(orderPaidTime);
			refundCommissionRateRequestBean.setUserId(buyerId);
			refundCommissionRateRequestBean.setSellerId(sellerId);
			refundCommissionRateRequestBean.setCountryId(countryId);
			refundCommissionRateRequestBean.setProductInfoList(productInfoList);
			
			refundCommissionRateServiceCall.setData(refundCommissionRateRequestBean);
			refundCommissionRateServiceCall.callService();
			this.verifyNormal(refundCommissionRateServiceCall, productInfoList);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_RefundCommissionRate_003() {
		Logger.start(true,"支付时间不传");
		try {
			Logger.comment("前置数据准备");
			long orderId = 11111;
			long buyerId = 1234;
			long sellerId = 22233;
			int countryId = 1;
			
			ArrayList<ProductInfoModel> productInfoList = new ArrayList<ProductInfoModel>();
			ProductInfoModel info1 = new ProductInfoModel();
			String catalogId = "catalogId1";
			String categoryId = "1001";
			String secCategoryId = "1102";
			BigDecimal productPrice = new BigDecimal(53);
			
			info1.setCatalogId(catalogId);
			info1.setCategoryId(categoryId);
			info1.setSecCategoryId(secCategoryId);
			info1.setProductPrice(productPrice);
			
			productInfoList.add(info1);
			Logger.comment("前置数据准备完毕");
			
			refundCommissionRateRequestBean.setOrderId(orderId);
			refundCommissionRateRequestBean.setUserId(buyerId);
			refundCommissionRateRequestBean.setSellerId(sellerId);
			refundCommissionRateRequestBean.setCountryId(countryId);
			refundCommissionRateRequestBean.setProductInfoList(productInfoList);
			
			refundCommissionRateServiceCall.setData(refundCommissionRateRequestBean);
			refundCommissionRateServiceCall.callService();
			
			Logger.verifyEquals("orderPaidTime:may not be null", 
					refundCommissionRateServiceCall.getJSONReturnData().getString("resultMessage"), 
					"验证resultMessage");
			Logger.verifyEquals("2", 
					refundCommissionRateServiceCall.getJSONReturnData().getString("resultCode"), 
					"验证resultCode");
			Logger.verifyEquals(false, 
					refundCommissionRateServiceCall.getJSONReturnData().getBoolean("success"), 
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	/**
	 * 一般性验证
	 * 
	 * @return
	 * @throws JSONException 
	 */
	public void verifyNormal(RefundCommissionRateServiceCall refundCommissionRateServiceCall, ArrayList<ProductInfoModel> productInfoList) throws JSONException {
		Logger.verifyEquals("success", 
				refundCommissionRateServiceCall.getJSONReturnData().getString("resultMessage"), 
				"验证resultMessage");
		Logger.verifyEquals("0", 
				refundCommissionRateServiceCall.getJSONReturnData().getString("resultCode"), 
				"验证resultCode");
		Logger.verifyEquals(true, 
				refundCommissionRateServiceCall.getJSONReturnData().getBoolean("success"), 
				"验证success");
		JSONArray productRefundCommissionList = refundCommissionRateServiceCall.getProductRefundCommissionList();
		
		//退佣接口走配置，统一输出，catalogId为请求的catalogId
		for (int i = 0; i < productRefundCommissionList.length(); i++) {
			Logger.verifyEquals(1, 
					productRefundCommissionList.getJSONObject(i).getInt("refundCommissionRate"), 
					"验证refundCommissionRate");
			Logger.verifyEquals(productInfoList.get(i).getCatalogId(), 
					productRefundCommissionList.getJSONObject(i).getString("catalogId"), 
					"验证catalogId");
			Logger.verifyEquals("bizConfig", 
					productRefundCommissionList.getJSONObject(i).getString("strategyId"),
					"验证strategyId");
		}
	}
}
